#!/bin/sh
#
# multimediamerge - merges multimedia development ports with FreeBSD ports
#
# Some parts came from marcusmerge.sh http://www.marcuscom.com/
#
# Copyright (c) 2006 Michael Johnson <ahze@FreeBSD.org>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
# $Id$

SRCDIR=${SRCDIR:="/tmp"}
	# The path to the Multimedia ports without the trailing
	# ports-multimedia
TMPDIR=${TMPDIR:="/tmp"}
PORTSDIR=${PORTSDIR:="/usr/ports"}
	# The path to the "official" ports collection
PORTSMODULE=${PORTSMODULE:="ports-multimedia"}

SVNROOT="http://ports-multimedia.googlecode.com/svn/trunk/ports-multimedia"
TARBALL_URL="http://people.freebsd.org/~ahze/"

# Script variables, nothing below needs to be changed.
MVER="20061030"
SCRIPTNAME=`basename $0`
VCDIR=".svn"

_download_only="no"
_update_ports="no"

echof()
{
	msg=$1
	echo "${msg}" | /usr/bin/fmt 75 75
}

usage()
{
cat <<EOF
usage: ${SCRIPTNAME} [-s] [-t] [-u] [-S <srcdir>] [-p <portsdir>] [-d]

-s Use subversion (svn) to update ports
-t Use a tarball from ${TARBALL_URL} to update ports
-u Sync ${PORTSMODULE} with ${PORTSDIR}. Default: On with -s or -t
-S Path to ${PORTSMODULE} with out trailing ${PORTSMODULE}
-p Path to portsdir. Deafult: ${PORTSDIR}
-d Download only (use with -s or -t) and don't sync with ${PORTSDIR}

EOF
}

update_tarball()
{
	fetch_out=`mktemp ${TMPDIR}/${PORTSMODULE}.tar.XXX`
	fetch -o ${fetch_out} ${TARBALL_URL}/${PORTSMODULE}.tar
	rm -rf ${SRCDIR}/${PORTSMODULE}
	tar -xf ${fetch_out} -C ${SRCDIR}
	rm -f ${fetch_out}
}

update_svn()
{
	which svn >/dev/null || 
	{
		echof "Error: svn not found! Please install subversion or use tarball update (-t)"
		exit 15
	}
	if [ -d ${SRCDIR}/${PORTSMODULE}/${VCDIR} ] ; then
		(cd ${SRCDIR}/${PORTSMODULE} && svn update)
	else 
		(cd ${SRCDIR} && rm -rf ${PORTSMODULE} && \
		svn checkout ${SVNROOT} ${PORTSMODULE})
	fi
}

update_ports()
{
  if [ -d ${SRCDIR}/${PORTSMODULE} ] ; then
	echof "===> Merging ${PORTSMODULE} into ${PORTSDIR}"
	mkdir -fp ${PORTSDIR}/Mk 2>/dev/null
     for mk in `ls -1 ${SRCDIR}/${PORTSMODULE}/Mk/*.mk 2>/dev/null` ; do
	cp -f ${mk} ${PORTSDIR}/Mk
     done
     for cat in `ls -1 ${SRCDIR}/${PORTSMODULE} 2>/dev/null` ; do
       if [ "${cat}" = "${VCDIR}" ] ; then
	continue
       fi
       for port in `ls -1 ${SRCDIR}/${PORTSMODULE}/${cat}`; do
         if [ ! -f ${SRCDIR}/${PORTSMODULE}/${cat}/${port}/Makefile ] ; then
		continue
         fi
       if [ -d ${PORTSDIR}/${cat}/${port} ] ; then
	rm -rf ${PORTSDIR}/${cat}/${port}/work
	find ${PORTSDIR}/${cat}/${port} \! -path "*/CVS/*" -type f | \
		xargs rm -f
       fi
	cd ${SRCDIR}/${PORTSMODULE}/${cat}
	tar --exclude "*/${VCDIR}*" -cf - ${port} | \
		tar -xf - -C ${PORTSDIR}/${cat}
       done
     done
  else
	echof "Error: ${PORTSMODULE} not found!"
	usage
  fi
}

while getopts "hdstuS:p:" a; do
    case "${a}" in
	d)
		_download_only="yes"
		;;
	s)
		_checkout="svn"
		;;
	t)
		_checkout="tarball"
		;;
	S)
		SRCDIR="${OPTARG}"
		;;
	p)
		PORTSDIR="${OPTARG}"
		;;
	u)
		_update_ports="yes"
		;;
	h | *)
		usage
		exit 1
    esac  
done

if [ ! -z "${_checkout}" ] ; then
	update_${_checkout}
   if [ "${_download_only}" = "no" ] ; then
	_update_ports="yes"
   fi
fi

if [ "${_update_ports}" = "yes" ] ; then
	update_ports
fi
